package com.example.star.service;

import com.example.star.entity.BuyNow;
import com.example.star.entity.ShoppingCart;
import com.example.star.mapper.BuyNowMapper;
import com.example.star.mapper.ShoppingCartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BuyNowService {
    @Autowired
    private BuyNowMapper buyNowMapper;
    public void saveBuyNow(BuyNow buyNow) {
        buyNowMapper.insertBuyNow(buyNow);
    }

    public List<BuyNow> getAllBuyNows() {
        return buyNowMapper.getAllBuyNows();
    }


    public List<BuyNow> getBuyNowByuserName(String userName){
        return buyNowMapper.getBuyNowByuserName(userName);
    }
    public List<BuyNow> getBuyNowByOrderStatus(String orderStatus){
        return buyNowMapper.getBuyNowByOrderStatus(orderStatus);
    }

    public List<BuyNow> getBuyNowsByuserNameAndOrderStatus(String userName,String orderStatus){
        return buyNowMapper.getBuyNowsByuserNameAndOrderStatus(userName,orderStatus);
    }


    @Transactional
    public BuyNow updateOrderStatus(int id, String orderStatus) {
        buyNowMapper.updateOrderStatus(id, orderStatus);
        return buyNowMapper.getBuyNowById(id);
    }

    public List<BuyNow> getBuyNowByPage(int page, int size) {
        int offset = (page - 1) * size;
        return buyNowMapper.getBuyNowsByPage(offset, size);
    }

    public int getTotalBuyNows() {
        return buyNowMapper.getTotalBuyNows();
    }

    public void deleteBuyNowById(int id) {
        buyNowMapper.deleteBuyNowById(id);
    }
    public String submitEvaluation(int id, float rating, String comment) {
        BuyNow buyNow = new BuyNow();
        buyNow.setId(id);
        buyNow.setRating(rating);
        buyNow.setComment(comment);

        int result = buyNowMapper.updateEvaluation(buyNow);
        if (result > 0) {
            return "评价提交成功";
        } else {
            return "评价提交失败";
        }
    }

    public void deleteOrder(int id) {
        buyNowMapper.deleteOrder(id);
    }

    // 根据用户名分页获取订单信息
    public List<BuyNow> getBuyNowsByUserName(String userName, int page, int size) {
        int offset = (page - 1) * size;
        return buyNowMapper.getBuyNowsByUserName(userName, offset, size);
    }

    // 根据用户名获取订单信息的总数
    public int getTotalBuyNowsByUserName(String userName) {
        return buyNowMapper.getTotalBuyNowsByUserName(userName);
    }
}
